<?php
require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR ."core".DIRECTORY_SEPARATOR."functions.php");
require_once("Database.php");

class Role extends dbConn
{
	private $roleId;
	private $title,$description;
	private $startTime,$endTime;
	private $startDate,$endDate;
	private $activity,$payRate;
	private $positions,$skills;

	/*
	
	*/

	public function __construct($roleId)
	{
		$this->roleId = $roleId;
		$this->fetchData();
	}

	public function fetchData()
	{
		$res = $this->query("SELECT * FROM role WHERE roleId=".$this->roleId);
		if($this->num_rows($res) == 1)
		{
			$data = $this->fetch_array($res);
			$this->title = $data['title'];
			$this->description = $data['description'];
			$this->startTime = $data['startTime'];
			$this->endTime = $data['endTime'];
			$this->startDate = $data['startDate'];
			$this->endDate = $data['endDate'];
			$this->activity = $data['activity'];
			$this->payRate = $data['payRate'];
			$this->positions = $data['positions'];
			$this->skills = $data['skills'];
		}
		else
		{
			die("NO DATA FROM roleId = $this->roleId");
		}
	}

	//Getters and Setters
	public function __get($var)
	{
		if(isset($this->$var))
		{
			return $this->$var;
		}
		else
		{
			return "NULL";
		}
	}

	public function apply($volunteerId)
	{
		$res = $this->INSERT("INSERT INTO applied (person,role) VALUES ($volunteerId,$this->roleId)");
		if($res)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public static function getPersonByRole($roleId)
	{
		$conn = new dbConn();
		$cnd = "role = $roleId";
		$query = "SELECT person FROM applied WHERE ".$cnd;
		$res = $conn->query($query);
		if($conn->num_rows($res) >= 1)
		{
			$array = array();
			while($row = $conn->fetch_array($res))
			{
				$array[] = $row;
			}
			return ($array);
		}
		else
		{
			return null;
		}
	}

	public static function getRoleByActivity($activityId,$assoc=false)
	{
		$conn = new dbConn();
		$cnd = "activity = $activityId";
		$query = "SELECT * FROM role WHERE ".$cnd;
		$res = $conn->query($query);
		if($conn->num_rows($res) >= 1)
		{
			$array = array();
			while($row = $conn->fetch_array($res,$assoc))
			{
				$array[] = $row;
			}
			return ($array);
		}
	}

	public static function getRolesByDate($startDate)
	{
		$conn = new dbConn();
		$cnd = "startDate>=$startDate";
		$query = "SELECT roleId,title FROM role WHERE ".$cnd;
		$res = $conn->query($query);
		if($conn->num_rows($res) >= 1)
		{
			$array = array();
			while($row = $conn->fetch_array($res))
			{
				$array[] = $row;
			}
			return ($array);
		}
		else
		{
			return null;
		}
	}
    
    public static function getAcceptedByRole($roleId,$assoc=false)
	{
		$db = new dbConn();
		$query = "SELECT person,role,accepted,attended,hoursWorked,applied.dateCreated,concat(firstName,' ',lastName) as name "
            . "FROM applied JOIN person ON applied.person=person.personId "
            . "WHERE role = $roleId AND accepted = 'accepted'";
		$res = $db->query($query);
		if($db->num_rows($res) >= 1)
		{
			$array = array();
			while($row = $db->fetch_array($res,$assoc))
			{
				$array[] = $row;
			}
			return ($array);
		}
		else
		{
			return null;
		}
	}
    
	public static function getAppliedByRole($roleId,$assoc=false)
	{
		$db = new dbConn();
		$cnd = "role = $roleId";
		$query = "SELECT * FROM applied WHERE ".$cnd;
		$res = $db->query($query);
		if($db->num_rows($res) >= 1)
		{
			$array = array();
			while($row = $db->fetch_array($res,$assoc))
			{
				$array[] = $row;
			}
			return ($array);
		}
		else
		{
			return null;
		}
	}

	public static function getRolesNum($activityId)
	{
		$db = new dbConn();
		$cnd = "activity=$activityId";
		$res = $db->query("SELECT * FROM role WHERE ".$cnd);
		return ($db->num_rows($res));
	}

	public static function getAppliedNum($roleId)
	{
		$db = new dbConn();
		$cnd = "role=$roleId";
		return ($db->num_rows($db->query("SELECT * FROM applied WHERE ".$cnd)));
	}

	public static function updateRole($roleId,$title,$description,$startTime,$endTime,$startDate,$endDate,$payRate,$positions,$skills,$activityId)
	{
		$dbConn = new dbConn();
		$res = $dbConn->query("UPDATE role SET title='$title',description='$description',startTime='$startTime',endTime='$endTime',startDate='$startDate',endDate='$endDate',payRate='$payRate',positions=$positions,skills='$skills',activity=$activityId WHERE roleId=$roleId");
	}

	public static function deleteRole($id,$isRole=true)
	{
		$dbConn = new dbConn();
		//Can be an activity id or a role id
		$cnd = "WHERE role=$id";
		/*
		if($dbConn->num_rows($dbConn->query("SELECT * FROM role_skill WHERE ".$cnd)) > 0)
		{
			$dbConn->delete("DELETE FROM role_skill WHERE role=$id");
		}
		*/
		$dbConn->delete("DELETE FROM role_skill WHERE EXISTS ( SELECT role_skill.role FROM role_skill WHERE role_skill.role=$id )");
		
		/*
		if($dbConn->num_rows($dbConn->query("SELECT * FROM applied WHERE ".$cnd)) > 0)
		{
			$dbConn->delete("DELETE FROM applied WHERE role=$id");
		}
		*/
		$dbConn->delete("DELETE FROM role_skill WHERE EXISTS ( SELECT applied.role FROM applied WHERE applied.role=$id )");
		if($isRole)
			{
				/*
				if($dbConn->num_rows($dbConn->query("SELECT * FROM role WHERE roleId=$id"))>0)
				{
					$dbConn->delete("DELETE FROM role WHERE role=$id");
				}*/
				$dbConn->delete("DELETE FROM role WHERE EXISTS ( SELECT role.roleId FROM role WHERE role.roleId=$id )");
		}
		else
		{
			/*
			if($dbConn->num_rows($dbConn->query("SELECT * FROM role WHERE activity=$id"))>0)
			{
				$dbConn->delete("DELETE FROM role WHERE activity=$id");
			}
			*/
			$dbConn->delete("DELETE FROM role WHERE ( SELECT role.activityId FROM role WHERE role.activityId=$id )");
		}
	}
    
    public static function exists($roleId)
	{
		$dbConn = new dbConn();
		if($dbConn->num_rows($dbConn->query("SELECT roleId FROM role WHERE roleId = $roleId")) > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public static function isAvailable($roleId)
	{
		$db = new dbConn();
		$query = $db->query("SELECT positions FROM role WHERE roleId=$roleId");
		$data = $db->fetch_array($query);
		if($data["positions"] <= 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
    
    public static function getAccepted($role,$person)
    {
        $db = new dbConn();
		$query = $db->query("SELECT accepted FROM applied WHERE role=$role AND person=$person");
        $data = $db->fetch_array($query);
        return $data["accepted"];
    }

	public static function applyPos($roleId,$personId)
	{
		$db = new dbConn();
		$query = $db->query("INSERT INTO applied (person,role,accepted,attended) VALUES ($personId,$roleId,'pending',0)");
		//self::subPosition($roleId,1);
	}

	public static function withdraw($roleId,$personId)
	{
		$db = new dbConn();
		$query = $db->query("DELETE FROM applied WHERE role=$roleId AND person=$personId");
		//self::addPosition($roleId,1);
	}

	public static function subPosition($roleId,$value)
	{
		$db = new dbConn();
		$query = $db->query("UPDATE role SET positions=positions-$value WHERE roleId=$roleId");
	}

	public static function addPosition($roleId,$value)
	{
		$db = new dbConn();
		$query = $db->query("UPDATE role SET positions=positions+$value WHERE roleId=$roleId");
	}

	public static function updateRoleAccepted($personId,$roleId,$accepted,$hours,$attended)
	{
		$db = new dbConn();
		$res = $db->query("UPDATE applied SET accepted='$accepted',hoursWorked='$hours',attended=$attended WHERE person=$personId AND role=$roleId");
	}

	public static function updateRoleApplied($personId,$roleId,$attended,$hoursWorked)
	{
		$db = new dbConn();
		$res = $db->query("UPDATE applied SET attended=$attended,hoursWorked='$hoursWorked' WHERE person=$personId AND role=$roleId");	
	}
}
?>